package com.jibei.mendo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jibei.mendo.R;

import java.util.List;

/**
 * Created by lhl on 2019/9/1.
 * 检测页面中的设备适配器
 */
public class CheckDevAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<String> strings;
    private CheckDevListener mCheckDevListener;

    public CheckDevAdapter(Context context, List<String> strings, CheckDevListener mCheckDevListener) {
        this.mContext = context;
        this.strings = strings;
        this.mCheckDevListener = mCheckDevListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_check_dev, viewGroup, false);
        return new CheckDevAdapter.CheckDevViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        String string = Integer.valueOf(strings.get(position), 16).toString();
        ((CheckDevAdapter.CheckDevViewHolder) viewHolder).mText.setText(string);
        ((CheckDevViewHolder) viewHolder).mButton.setOnClickListener(v -> mCheckDevListener.onCheckDevListener(v, position));
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    private class CheckDevViewHolder extends RecyclerView.ViewHolder {

        private TextView mText;
        private Button mButton;

        public CheckDevViewHolder(@NonNull View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id.tv_dev_name);
            mButton = itemView.findViewById(R.id.btn_check_dev);
        }
    }

    public interface CheckDevListener {
        void onCheckDevListener(View mView, int position);
    }
}
